package com.nidu.demo.tenant.model;

import com.alibaba.cola.exception.BizException;
import com.nidu.demo.base.BaseDeletableEntity;
import com.nidu.demo.base.BaseEntity;
import com.nidu.demo.base.BaseStatusEntity;
import com.nidu.demo.common.enums.BooleanEnum;
import com.nidu.demo.common.enums.StatusEnum;
import com.nidu.demo.common.exception.ErrorCodeConstants;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.experimental.Accessors;

import java.time.LocalDateTime;

/**
 * 租户领域对象
 *
 * @author auto
 */
@Data
@Accessors(chain = true)
@EqualsAndHashCode(callSuper = true)
public class Tenant extends BaseEntity implements BaseStatusEntity, BaseDeletableEntity {

    private Long id;

    /**
     * 租户名称
     */
    private String name;

    /**
     * 租户管理员ID
     */
    private Long adminId;

    /**
     * 租户管理员名称
     */
    private String adminName;

    /**
     * 租户管理员手机号码
     */
    private String adminMobile;

    /**
     * 绑定域名
     */
    private String website;

    /**
     * 租户套餐ID
     */
    private Long packageId;

    /**
     * 过期时间
     */
    private LocalDateTime expireTime;

    /**
     * 用户数量限制
     */
    private Integer accountCount;

    /**
     * 描述
     */
    private String remark;

    /**
     * 排序值
     */
    private Integer sortOrder;

    /**
     * 状态
     */
    private Integer status;

    /**
     * 是否删除
     */
    private Boolean deleted;

    /**
     * 创建租户
     */
    public static Tenant create(String name, String adminName, String adminMobile, String website,
                              Long packageId, LocalDateTime expireTime, Integer accountCount,
                              String remark, Integer sortOrder) {
        Tenant tenant = new Tenant();
        tenant.setName(name)
                .setAdminName(adminName)
                .setAdminMobile(adminMobile)
                .setWebsite(website)
                .setPackageId(packageId)
                .setExpireTime(expireTime)
                .setAccountCount(accountCount)
                .setRemark(remark)
                .setSortOrder(sortOrder);
        return tenant;
    }

    /**
     * 更新租户
     */
    public static Tenant update(Long id, String name, Long adminId, String website, Long packageId,
                                LocalDateTime expireTime, Integer accountCount,
                              String remark, Integer sortOrder) {
        Tenant tenant = new Tenant();
        tenant.setId(id)
                .setName(name)
                .setAdminId(adminId)
                .setWebsite(website)
                .setExpireTime(expireTime)
                .setAccountCount(accountCount)
                .setRemark(remark)
                .setSortOrder(sortOrder);
        return tenant;
    }

    @Override
    public void isDeleted() {
        if (BooleanEnum.TRUE.getValue().equals(this.getDeleted())) {
            throw new BizException(ErrorCodeConstants.TENANT_NOT_FOUND.getCode(), ErrorCodeConstants.TENANT_NOT_FOUND.getMessage());
        }
    }

    @Override
    public void isDisable() {
        if (StatusEnum.DISABLE.getValue().equals(this.status)) {
            throw new BizException(ErrorCodeConstants.TENANT_STATUS_DISABLE.getCode(), ErrorCodeConstants.TENANT_STATUS_DISABLE.getMessage());
        }
    }

    /**
     * 检查租户是否过期
     */
    public void isExpired() {
        if (this.expireTime != null && LocalDateTime.now().isAfter(this.expireTime)) {
            throw new BizException(ErrorCodeConstants.TENANT_EXPIRED.getCode(), ErrorCodeConstants.TENANT_EXPIRED.getMessage());
        }
    }

    /**
     * 检查用户数量是否超限
     * @param currentUserCount 当前用户数量
     */
    public void checkAccountCount(int currentUserCount) {
        if (this.accountCount != null && currentUserCount >= this.accountCount) {
            throw new BizException(ErrorCodeConstants.TENANT_ACCOUNT_COUNT_EXCEED.getCode(),
                    ErrorCodeConstants.TENANT_ACCOUNT_COUNT_EXCEED.getMessage());
        }
    }
}